Conditions | 7 |
Paths | 11 |
Total Lines | 51 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** global: UB */ |
||
30 | replaceBetween: function(findStart, findEnd, replaceWith, keepEnds = true, startAt = 0, times = 0){ |
||
31 | var s = this.toString(); |
||
32 | |||
33 | var start; |
||
34 | var last = 0; |
||
35 | var done = 0; |
||
36 | |||
37 | if ((start = s.indexOf(findStart)) > -1){ |
||
38 | |||
39 | // for each instance of marker |
||
40 | while (true) { |
||
41 | |||
42 | // exit if end marker not in text |
||
43 | var end = s.indexOf(findEnd, start + findStart.length); |
||
44 | if (end === -1) { |
||
45 | return s; |
||
46 | } |
||
47 | |||
48 | // get code before/after markers |
||
49 | var partA = s.substr(0, start); |
||
50 | var partB = s.substr(end + findEnd.length); |
||
51 | |||
52 | // merge code with new value inplace of markers |
||
53 | if (keepEnds) { |
||
54 | s = partA + findStart + replaceWith + findEnd; |
||
55 | }else { |
||
56 | s = partA + replaceWith; |
||
57 | } |
||
58 | |||
59 | // move forward |
||
60 | last = s.length; |
||
61 | |||
62 | // add ending text |
||
63 | s += partB; |
||
64 | |||
65 | // find next marker |
||
66 | start = s.indexOf(findStart, last); |
||
67 | if (start === -1) { |
||
68 | break; |
||
69 | } |
||
70 | |||
71 | // only replace certain number of times |
||
72 | done++; |
||
73 | if (done == times) { /// ignore "times" if 0 |
||
74 | break; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | } |
||
79 | return s; |
||
80 | }, |
||
81 | replaceMany: function(findArray, replaceArray, wholeWords = false, caseSensitive = true){ |
||
176 | UB.registerFuncs(String.prototype, stringFuncs); |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: